#Requires AutoHotkey v2.0

global ShowFunc := ""     ;计时器标签名

FormatSeconds(NumberOfSeconds)  ; 把指定的秒数转换成 hh:mm:ss 格式.
{
    time := A_Year . A_Mon . A_MDay  ; 任意日期的 *午夜*.
    time := DateAdd(time, NumberOfSeconds, "Seconds")
    hours :=(NumberOfSeconds//3600) ? NumberOfSeconds//3600 ":" : ""
    return  hours . FormatTime(time, "mm:ss")
}

;显示当前时间
curentTime() {
  weekday := ["Sunday", "Monday", "Tuseday", "Wednesday", "Thursday", "Friday", "Saturday"]
  
  OnlyInput.fixedTip(A_Hour . ":" . A_Min . "`n" . A_MDay . "/" . A_Mon
"`n" . weekday[A_WDay], 6000)

}

;切换计时器状态
toggleShowTimmer(toggle) {
  global ShowFunc
  ShowFunc := toggle
  if !(RegExMatch(toggle, "^p"))
    SetTimer(()=>ShowFunc := "", -5000)
}

;计时器
funcTimmer(min) {
  global ShowFunc
  static TimmerMap := Map() 

  countDown() {
    toolTipTimmerMap() {
      for k,v in TimmerMap {
        strTooltip .=  k . "   " . v  . "`n"
        }
        OnlyInput.fixedTip(strTooltip, 1000)
    }
    if (ShowFunc = "p " . tag) or ShowFunc = "pause" {
      toolTipTimmerMap()
    } else {    ;暂停计时器
      second -= 1
      TimmerMap[tag] := FormatSeconds(second)
      if (ShowFunc = tag) {          ;显示剩余时间n
        OnlyInput.fixedTip(FormatSeconds(second), 1000)
      } else if (ShowFunc = "c " . tag or ShowFunc = "clear") {  ;清除计时器
        TimmerMap.Delete(tag)
        SetTimer(countDown, 0)
      } else if (ShowFunc = "s" or ShowFunc = "show") {
        toolTipTimmerMap()
      } 
      if (second < 1) {
        SetTimer(countDown, 0)
        TimmerMap.Delete(tag)
        SoundPlay("assets\xm2752.mp3")
        MsgBox(tag . "  time over", "timmer定时器", "262144")
      }
    }
  }

  minArray := StrSplit(min, " ")         ;拆分计时器标签和时间
  if minArray.Length < 2 {
    minArray.InsertAt(1, min)
  } 
  tag := minArray[1]
  second := Round(minArray[2] * 60)
  TimmerMap[tag] := FormatSeconds(second)
  OnlyInput.fixedTip("tag: " . tag . "`ncountdown: " . TimmerMap[tag], 3500)
  SetTimer(countDown, 1000)
}